package com.summer.media.controller;


import com.summer.commonException.XueChengException;
import com.summer.media.dto.UploadParamsDto;
import com.summer.media.service.MediaFileService;
import com.summer.model.dto.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.math.raw.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;

@Api(tags = "大文件处理API")
@RestController
@Slf4j
public class VideoFilesController {

    @Autowired
    private MediaFileService mediaFileService;

    /**
     * 判断视频是否存在
     * @param fileMd5 MD5
     * @return 返回RestResponse.success(false)表示视频需要上传，RestResponse.success(true, msg)表示不需要上传
     */
    @ApiOperation(value = "文件上传前检查文件")
    @PostMapping("/upload/checkfile")
    public RestResponse<Boolean> isFileExist(String fileMd5){
        return mediaFileService.isFileExist(fileMd5);
    }

    /**
     * 判断分块是否存在
     * @param fileMd5 文件MD5值
     * @param chunk 分块名
     * @return 返回RestResponse.success(false)表示分块需要上传，RestResponse.success(true)表示不需要上传
     */
    @ApiOperation(value = "分块文件上传前的检测")
    @PostMapping("/upload/checkchunk")
    public RestResponse<Boolean> isBlockExist(String fileMd5, int chunk){
        return mediaFileService.isBlockExist(fileMd5, chunk);
    }

    /**
     * 上传视频
     * @param blockFile 分块文件别名，注解中的值要与前端上传文件的name保持一致
     * @param fileMd5 MD5
     * @param chunk 分块名
     * @return 返回RestResponse.validfail(false, msg)表示分块上传失败需要重新上传，RestResponse.success(true)表示分块上传成功
     */
    @ApiOperation(value = "上传分块")
    @PostMapping("/upload/uploadchunk")
    public RestResponse<Boolean> uploadBlockFile(@RequestPart("file") MultipartFile blockFile,
                                        String fileMd5,
                                        int chunk){
        File tempFile = null;
        try {
            tempFile = File.createTempFile("minio", "temp");
            tempFile.deleteOnExit();
            blockFile.transferTo(tempFile);
        }catch (IOException e){
            log.error("【上传视频分块】创建临时文件失败");
            e.printStackTrace();
            XueChengException.cast("【上传视频分块】创建临时文件失败");
        }
        String localTemp = tempFile.getAbsolutePath();
        return mediaFileService.uploadBlockFile(fileMd5, chunk, localTemp);
    }

    /**
     * 合并分块
     * @param fileMd5 源文件MD5
     * @param chunkTotal 分块数量
     * @param fileName 源文件名
     * @return
     */
    @ApiOperation(value = "合并文件")
    @PostMapping("/upload/mergechunks")
    public RestResponse<Boolean> mergeBlock(String fileMd5, int chunkTotal, String fileName){
        Long companyId = 1232141425L;

        UploadParamsDto uploadParamsDto = new UploadParamsDto();
        uploadParamsDto.setFileType("001002");
        uploadParamsDto.setTags("课程视频");
        uploadParamsDto.setRemark("");
        uploadParamsDto.setFilename(fileName);
        // 文件大小在impl中设置
        return mediaFileService.mergeBlock(companyId,fileMd5,chunkTotal, uploadParamsDto);
    }

    public ModelAndView getVideoPage(ModelAndView modelAndView){


        return modelAndView;
    }
}
